package com.crm.system.controller.contactManager;

import com.alibaba.fastjson.JSONObject;
import com.crm.common.model.DataResponse;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.DhResponse;
import com.crm.common.model.Enum.ResponseCodeEnum;
import com.crm.model.entity.cluePoolManager.ClueFollow;
import com.crm.model.entity.contactManager.*;
import com.crm.model.entity.customerManager.Customer;
import com.crm.model.entity.opportunityManager.Opportunity;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.entity.systemManager.SysParameterSetting;
import com.crm.model.qo.cluePoolManager.ClueFollowQO;
import com.crm.model.qo.contactManager.AddContactInformationQO;
import com.crm.model.qo.contactManager.ContactFollowPlanTimeQO;
import com.crm.model.qo.contactManager.ContactQO;
import com.crm.model.vo.cluePoolManager.ClueFollowVO;
import com.crm.model.vo.cluePoolManager.ContactDetailFollowVO;
import com.crm.model.vo.contactManager.*;
import com.crm.model.vo.customerContractManager.ContactDetailByCustomerContractVO;
import com.crm.model.vo.customerManager.CustomerContactVO;
import com.crm.model.vo.customerManager.FzCustomerContactVO;
import com.crm.model.vo.opportunityManager.OpportunityVO;
import com.crm.model.vo.systemManager.CardImportVO;
import com.crm.model.vo.systemManager.ContactImportVO;
import com.crm.model.vo.systemManager.UserAccountImportVO;
import com.crm.system.service.cluePoolManager.ClueFollowService;
import com.crm.system.service.cluePoolManager.ExcelUtil;
import com.crm.system.service.contactManager.ContactDetailService;
import com.crm.system.service.contactManager.ContactInformationService;
import com.crm.system.service.contactManager.ContactService;
import com.crm.system.service.contractManager.CustomerContractService;
import com.crm.system.service.customerManager.CustomerService;
import com.crm.system.service.impl.cluePoolManager.ClueFollowServiceImpl;
import com.crm.system.service.opportunityManager.OpportunityService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * 联系人管理信息
 */
@RestController
@RequestMapping("/contact")
@Slf4j
public class ContactController {

    @Autowired
    private ContactService contactService;

    @Autowired
    private ContactDetailService contactDetailService;

    @Autowired
    private OpportunityService opportunityService;

    @Autowired
    private ContactInformationService contactInformationService;

    @Autowired
    private ClueFollowService clueFollowService;

    @Autowired
    private CustomerContractService customerContractService;

    @Autowired
    private CustomerService customerService;

    /**
     * 管理模块 -- 联系管理列表
     *
     * @param pageIndex 页码
     * @param pageSize  分页大小
     * @param sortName  排序列名字
     * @param sortOrder 排序规则(asc/desc)
     * @param contactQO 查询条件
     * @return
     */
    @PostMapping("/contactList")
    public DataResponse customerPoolList(HttpServletRequest request,
                                         @RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                         @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                         @RequestParam(value = "sortName", required = false) String sortName,//排序列名字
                                         @RequestParam(value = "sortOrder", required = false) String sortOrder,//排序规则(asc/desc)
                                         ContactQO contactQO//查询条件
    ) {
        try {
            DhPageInfo<ContactVO> contactVOList = contactService.selectContactVOList(pageIndex, pageSize, contactQO, sortName, sortOrder);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), contactVOList);
        } catch (Exception e) {
            log.error("contact contactList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 联系人-新增联系人  联系人大表和客户下联系人同时新增
     *
     * @param customerContactVO 联系人实体类
     * @return
     */
    @PostMapping("/addContact")
    public Map<String, String> addContact(@RequestBody CustomerContactVO customerContactVO) {
        Map<String, String> map = new HashMap<String, String>();
        String contactId = contactService.addContact(customerContactVO);
        if (!StringUtils.isBlank(contactId) || !StringUtils.isBlank(customerContactVO.getContactId())) {  //有值
            map.put("code", "000000");
            map.put("message", "成功");
            map.put("contactId", contactId);
            return map;
        } else {
            map.put("code", "900000");
            map.put("message", "服务器错误");
            map.put("contactId", "");
            return map;
        }
    }

    /**
     * 联系人-新增联系人--同一客户下联系人去重
     *
     * @param customerId         客户id
     * @param contactName        联系人姓名
     * @param contactInformation 联系方式
     * @param contactNature      联系人性质  0企业联系人  1非企业联系人
     * @return
     */
    @PostMapping("/getCustomerIdAndContactNameAndAccountId")
    public DataResponse getCustomerIdAndContactNameAndAccountId(@RequestParam(value = "customerId", required = false) String customerId,
                                                                @RequestParam(value = "contactName", required = true) String contactName,
                                                                @RequestParam(value = "contactInformation", required = true) String contactInformation,
                                                                @RequestParam(value = "contactNature", required = true) String contactNature,
                                                                @RequestParam(value = "csd", required = false) String csd) {
        try {
            Contact contact = contactService.getCustomerIdAndContactNameAndAccountId(customerId, contactName, contactInformation, contactNature,csd);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), contact);
        } catch (Exception e) {
            log.error("contact getCustomerIdAndContactNameAndAccountId error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 联系人-删除 逻辑删除
     *
     * @param contactId 联系人id
     * @return
     */
    @PostMapping("/deleteContact")
    public DataResponse deleteCustomer(@RequestParam(value = "contactId", required = true) String contactId) {
        try {
            Boolean result = contactService.deleteContact(contactId);
            if (result) {
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
            }
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        } catch (Exception e) {
            log.error("contact deleteContact error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 回显联系人详情
     *
     * @param contactId 联系人id
     * @return
     */
    @PostMapping("/selectContactDetailById")
    public DataResponse selectContactDetailById(@RequestParam(value = "contactId", required = false) String contactId) {
        try {
            ContactVO contact = contactService.selectContactDetailById(contactId);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), contact);
        } catch (Exception e) {
            log.error("contact selectContactDetailById error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 修改回显联系人
     *
     * @param contactId 联系人id
     * @return
     */
    @PostMapping("/selectContactById")
    public DataResponse selectContactById(@RequestParam(value = "contactId", required = false) String contactId) {
        try {
            Contact contact = contactService.selectContactById(contactId);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), contact);
        } catch (Exception e) {
            log.error("contact selectByCustomerId error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 联系人-联系人详情-修改联系人详情
     * @param contactId 联系人id
     * @param name 图片  修改联系人评分  标签
     * @param bs 1 上传图片  2修改联系人评分  3标签
     * @return
     */
    @PostMapping("/updateContact")
    public DataResponse updateContact(@RequestParam(value = "contactId", required = false) String contactId,
                                      @RequestParam(value = "name", required = false) String name,
                                      @RequestParam(value = "bs", required = false) String bs) {
        Boolean result = contactService.updateContact(contactId,name,bs);
        if (result) {
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        } else {
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 联系人-联系人详情-修改联系人详情  家庭住址,公司地址,微信,邮箱
     * @param contact 联系人信息
     * @return
     */
    @PostMapping("/updateContactDetails")
    public DataResponse updateContactDetails(@RequestBody Contact contact) {
        Boolean result = contactService.updateContactDetail(contact);
        if (result) {
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        } else {
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 根据联系人id,客户id,联系人姓名,企业性质查询同一联系人下的联系方式
     *
     * @param customerContactVO 查询需要的实体类
     * @return
     */
    @PostMapping("/contactDetailList")
    public DataResponse contactDetailList(
            @RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
            @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
            @RequestParam(value = "sortName", required = false) String sortName,//排序列名字
            @RequestParam(value = "sortOrder", required = false) String sortOrder,//排序(asc/desc)
            CustomerContactVO customerContactVO) {
        try {
            DhPageInfo<ContactDetail> list = contactDetailService.contactDetailList(pageIndex, pageSize, sortName, sortOrder, customerContactVO);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), list);
        } catch (Exception e) {
            log.error("contact modificationRecordList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 根据联系人id查询商机
     *
     * @param contactId 联系人id
     * @return
     */
    @PostMapping("/opportunityDetailList")
    public DataResponse opportunityDetailList(
            @RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
            @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
            @RequestParam(value = "sortName", required = false) String sortName,//排序列名字
            @RequestParam(value = "sortOrder", required = false) String sortOrder,//排序(asc/desc)
            @RequestParam(value = "contactId", required = false) String contactId) {
        try {
            DhPageInfo<OpportunityVO> list = opportunityService.opportunityDetailList(pageIndex, pageSize, sortName, sortOrder, contactId);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), list);
        } catch (Exception e) {
            log.error("contact modificationRecordList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }


    /**
     * 联系人-新增联系方式,节日节点
     *
     * @param contactDetail 联系人实体类
     * @return
     */
    @PostMapping("/addContactDetail")
    public DataResponse addContactDetail(@RequestBody ContactDetail contactDetail) {
        Boolean result = contactDetailService.addContactDetail(contactDetail);
        if (result) {
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        } else {
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 联系人-联系人详情-联系方式重复
     *
     * @param contactId          联系人id
     * @param contactDetailValue 联系方式
     * @return
     */
    @PostMapping("/getContactDetailIdAndContactDetailValue")
    public DataResponse getContactDetailIdAndContactDetailValue(@RequestParam(value = "contactId", required = false) String contactId,
                                                                @RequestParam(value = "contactDetailValue", required = false) String contactDetailValue) {

        try {
            ContactDetail contactDetail = contactDetailService.getContactDetailIdAndContactDetailValue(contactId, contactDetailValue);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), contactDetail);
        } catch (Exception e) {
            log.error("contact getContactDetailIdAndContactDetailValue error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }


    /**
     * 联系人-联系人详情-联系方式,节日节点修改回显
     *
     * @param contactDetailId 联系方式id,节日节点id
     * @return
     */
    @PostMapping("/getContactDetailId")
    public DataResponse getContactDetailId(@RequestParam(value = "contactDetailId", required = false) String contactDetailId) {

        try {
            ContactDetail contactDetail = contactDetailService.getContactDetailId(contactDetailId);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), contactDetail);
        } catch (Exception e) {
            log.error("contact getContactDetailId error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 联系人-修改联系方式,节日节点
     *
     * @param contactDetail 联系方式,节日节点实体类
     * @return
     */
    @PostMapping("/updateContactDetail")
    public DataResponse updateContactDetail(@RequestBody ContactDetail contactDetail) {
        Boolean result = contactDetailService.updateContactDetail(contactDetail);
        if (result) {
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        } else {
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 联系人-删除联系方式,节日节点  逻辑删除
     *
     * @param contactDetailId 联系方式id,节日节点id是同一id
     * @return
     */
    @PostMapping("/deleteContactDetail")
    public DataResponse deleteContactDetail(@RequestParam(value = "contactDetailId", required = false) String contactDetailId) {
        Boolean result = contactDetailService.deleteContactDetail(contactDetailId);
        if (result) {
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        } else {
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 联系人-新增联系方式
     *
     * @param contactInformation 联系方式实体类
     * @return
     */
    @PostMapping("/addContactInformation")
    public DataResponse addContactInformation(@RequestBody ContactInformationVO contactInformation) {
        if(!StringUtils.isBlank(contactInformation.getExtNumber())) {
            contactInformation.setContactInformation(contactInformation.getContactInformation().replaceAll("-","") + " - " + contactInformation.getExtNumber());
        }else{
            contactInformation.setContactInformation(contactInformation.getContactInformation().replaceAll("-",""));
        }

        Boolean result = contactInformationService.addContactInformation(contactInformation);
        if (result) {
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        } else {
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 联系人-新增联系方式-联系方式重复
     *
     * @param contactId          联系人id
     * @param contactInformation 联系方式
     * @return
     */
    @PostMapping("/getContactIdAndContactInformation")
    public DataResponse getContactIdAndContactInformation(@RequestParam(value = "contactId", required = false) String contactId,
                                                          @RequestParam(value = "contactInformation", required = false) String contactInformation) {

        try {
            ContactInformation lxfs = contactInformationService.getContactIdAndContactInformation(contactId, contactInformation);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), lxfs);
        } catch (Exception e) {
            log.error("contact getContactIdAndContactInformation error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 联系人-新增联系人--同一客户下联系人重名-->合并联系方式
     *
     * @param customerId    客户id
     * @param contactName   联系人姓名
     * @param contactNature 联系人性质  0企业联系人  1非企业联系人
     * @return
     */
    @PostMapping("/getContactName")
    public DataResponse getContactName(@RequestParam(value = "customerId", required = true) String customerId,
                                       @RequestParam(value = "contactName", required = true) String contactName,
                                       @RequestParam(value = "contactNature", required = true) String contactNature) {
        try {
            List<Contact> list = contactService.getContactName(customerId, contactName, contactNature);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), list);
        } catch (Exception e) {
            log.error("contact getContactName error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 根据联系人id查询联系人下的联系方式
     *
     * @param pageIndex 页码
     * @param pageSize  分页大小
     * @param sortName  排序列名字
     * @param sortOrder 排序(asc/desc)
     * @param contactId 联系人id
     * @return
     */
    @PostMapping("/contactInformationList")
    public DataResponse contactInformationList(
            @RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
            @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
            @RequestParam(value = "sortName", required = false) String sortName,//排序列名字
            @RequestParam(value = "sortOrder", required = false) String sortOrder,//排序(asc/desc)
            @RequestParam(value = "contactId", required = false) String contactId) {
        try {
            DhPageInfo<ContactInformation> list = contactInformationService.contactInformationList(pageIndex, pageSize, sortName, sortOrder, contactId);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), list);
        } catch (Exception e) {
            log.error("contact contactInformationList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 联系人详情--联系方式 开启/废止联系方式
     *
     * @param fzCustomerContactVO contactInformationId数组 联系方式id
     * @param fzCustomerContactVO abolishState 废止状态  1启动 2废止
     * @return
     */
    @PostMapping("/updateAbolishState")
    public DataResponse updateAbolishState(@RequestBody FzCustomerContactVO fzCustomerContactVO) {
        try {
            Boolean result = contactService.updateAbolishStateById(fzCustomerContactVO);
            if (result) {
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
            }
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        } catch (Exception e) {
            log.error("customer updateAbolishState error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 联系人列表--废止联系人 开启/废止联系方式    废止联系人,联系方式,客户下联系人
     * @param fzCustomerContactVO abolishState 废止状态  1启动 2废止
     * @return
     */
    @PostMapping("/updateContactAbolishState")
    public DataResponse updateContactAbolishState(@RequestBody FzCustomerContactVO fzCustomerContactVO) {
        try {
            Boolean result = contactService.updateContactAbolishState(fzCustomerContactVO);
            if (result) {
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
            }
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        } catch (Exception e) {
            log.error("customer updateContactAbolishState error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }


    /**
     * 联系人-新增联系人--同一客户下联系方式不能一样
     *
     * @param customerId         客户id
     * @param contactInformation 联系方式
     * @param contactNature      联系人性质  0企业联系人  1非企业联系人
     * @return
     */
    @PostMapping("/getCustomerIdAndContactInformation")
    public DataResponse getCustomerIdAndContactInformation(@RequestParam(value = "customerId", required = true) String customerId,
                                                           @RequestParam(value = "contactInformation", required = true) String contactInformation,
                                                           @RequestParam(value = "contactNature", required = true) String contactNature) {
        try {
            List<ContactInformationVO> list = contactService.getCustomerIdAndContactInformation(customerId, contactInformation, contactNature);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), list);
        } catch (Exception e) {
            log.error("contact getCustomerIdAndContactInformation error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 联系人-客户详情-联系人列表-跳转联系人详情
     *
     * @param customerId         客户id
     * @param contactInformation 联系方式
     * @param contactName        联系人姓名
     * @return
     */
    @PostMapping("/getContactId")
    public DataResponse getContactId(@RequestParam(value = "customerId", required = true) String customerId,
                                     @RequestParam(value = "contactInformation", required = true) String contactInformation,
                                     @RequestParam(value = "contactName", required = true) String contactName) {
        try {
            Contact contact = contactService.getContactId(customerId, contactInformation, contactName);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), contact);
        } catch (Exception e) {
            log.error("contact getContactId error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 联系人详情-跟进记录查询-通过联系人id
     *
     * @param contactId 联系人id
     * @param dateType  thisWeek 本周  thisMonth 本月
     * @return
     */
    @PostMapping("/selectFollowByContactId")
    public DataResponse selectFollowByContactId(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                                @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                                @RequestParam(value = "sortName", required = false) String sortName,//排序列名字
                                                @RequestParam(value = "sortOrder", required = false) String sortOrder,//排序(asc/desc)
                                                @RequestParam(value = "contactId", required = true) String contactId,
                                                @RequestParam(value = "dateType", required = true) String dateType) {
        try {
            DhPageInfo<ContactDetailFollowVO> list = clueFollowService.selectFollowByContactId(pageIndex, pageSize, sortName, sortOrder, contactId, dateType);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), list);
        } catch (Exception e) {
            log.error("contact selectFollowByContactId error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 联系人详情-订单合同-通过联系人id
     *
     * @param pageIndex      页码
     * @param pageSize       分页大小
     * @param sortName       排序列名字
     * @param sortOrder      排序
     * @param contactId      联系人id
     * @param contractNumber 合同编号
     * @return
     */
    @PostMapping("/selectCustomerContractByContactId")
    public DataResponse selectCustomerContractByContactId(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                                          @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                                          @RequestParam(value = "sortName", required = false) String sortName,//排序列名字
                                                          @RequestParam(value = "sortOrder", required = false) String sortOrder,//排序(asc/desc)
                                                          @RequestParam(value = "contactId", required = true) String contactId,
                                                          @RequestParam(value = "contractNumber", required = false) String contractNumber) {
        try {
            DhPageInfo<ContactDetailByCustomerContractVO> list = customerContractService.selectCustomerContractByContactId(pageIndex, pageSize, sortName, sortOrder, contactId, contractNumber);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), list);
        } catch (Exception e) {
            log.error("contact selectCustomerContractByContactId error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * @param clueFollowQO 跟进参数
     * @return
     * @author wll
     * info 联系人管理-联系人列表-跟进
     */
    @PostMapping("/addContactFollow")
    public DataResponse addContactFollow(HttpServletRequest request,
                                         @RequestBody ClueFollowQO clueFollowQO) {
        try {
            Boolean result = contactService.addContactFollow(clueFollowQO);
            if (result) {
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
            }
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        } catch (Exception e) {
            log.error("clueFollow addClueFollow error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

//    @RequestMapping(value = "/index")
//    @ResponseBody
//    public String index() throws IOException {
//        CopyOnWriteArraySet<WebSocketServer> webSocketSet = WebSocketServer.webSocketSet;
//        for(WebSocketServer webSocketServer : webSocketSet){
//            webSocketServer.sendMessage("你好客户端.....我是服务端,我来了..");
//        }
//
//        return "200";
//    }

    /**
     * 查询联系人里面有的行业
     *
     * @param bs 0管理端 1用户端
     * @return
     */
    @PostMapping("/selectIndustry")
    public DataResponse selectIndustry(@RequestParam(value = "bs", required = true) String bs) {
        try {
            List<IndustryAreaVO> list = contactService.selectIndustry(bs, "industry");
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), list);
        } catch (Exception e) {
            log.error("contact selectIndustry error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 查询联系人里面有的地区
     *
     * @param bs 0管理端 1用户端
     * @return
     */
    @PostMapping("/selectProvince")
    public DataResponse selectProvince(@RequestParam(value = "bs", required = true) String bs) {
        try {
            List<IndustryAreaVO> list = contactService.selectIndustry(bs, "province");
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), list);
        } catch (Exception e) {
            log.error("contact selectIndustry error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 查询联系人里面有的标签
     * @param bs 0管理端 1用户端
     * @return
     */
    @PostMapping("/selectTag")
    public DataResponse selectTag(@RequestParam(value = "bs", required = true) String bs) {
        try {
            List<String> list = contactService.selectTag(bs);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), list);
        } catch (Exception e) {
            log.error("contact selectTag error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 联系人列表-设置为常用联系人
     *
     * @param contactId  联系人id
     * @param topContact 常用联系人  0常用  1不常用
     * @return
     */
    @PostMapping("/setUpTopContact")
    public DataResponse setUpTopContact(@RequestParam(value = "contactId", required = true) String contactId,
                                        @RequestParam(value = "topContact", required = true) Integer topContact) {
        try {
            Boolean result = contactService.setUpTopContact(contactId, topContact);
            if (result) {
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
            }
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        } catch (Exception e) {
            log.error("clueFollow setUpTopContact error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }


    /**
     * 联系人报表-折线图 -只有用户端
     *
     * @param dateType 1、本周-按天  2、本月-按天  3、上月-按天  4、近三月-按周  5、本季度-按周  6、本年度-按月
     * @param bs       0管理端 1用户端
     * @return
     */
    @PostMapping("/getContactReportLine")
    public DataResponse getContactReportLine(@RequestParam(value = "dateType",required = false, defaultValue = "1") Integer dateType,
                                             @RequestParam(value = "bs", required = false) String bs) {
        ContactReportLineVO reportLineVO = contactService.getContactReportLine(dateType, bs);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), reportLineVO);
    }


    /**
     * 联系人报表-列表 -只有用户端
     *
     * @param dateType  1、本周-按天  2、本月-按天  3、上月-按天  4、近三月-按周  5、本季度-按周  6、本年度-按月
     * @param pageIndex 页码
     * @param pageSize  分页大小
     * @param sortName  排序列名字
     * @param sortOrder 排序规则(asc/desc)
     * @param bs        0管理端 1用户端
     * @return
     */
    @PostMapping("/getContactReportList")
    public DataResponse getContactReportList(@RequestParam(value = "dateType") Integer dateType,
                                             @RequestParam(value = "pageIndex", required = false, defaultValue = "1") Integer pageIndex,
                                             @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                                             @RequestParam(value = "sortName", required = false) String sortName,
                                             @RequestParam(value = "sortOrder", required = false) String sortOrder,
                                             @RequestParam(value = "bs",required = false) String bs) {

        DhPageInfo<ContactReportListVO> reportList = contactService.getContactReportList(pageIndex, pageSize, sortName,
                sortOrder, dateType);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), reportList);
    }

    /**
     * 联系人-新增个人联系人--查询同一用户下的联系方式不能一样
     * @param contactName         客户名称
     * @param contactInformation 联系方式
     * @param contactNature      联系人性质  0企业联系人  1非企业联系人
     * @return
     */
    @PostMapping("/getCustomerNameAndPhone")
    public DataResponse getCustomerNameAndPhone(@RequestParam(value = "contactName", required = true) String contactName,
                                                           @RequestParam(value = "contactInformation", required = true) String contactInformation,
                                                           @RequestParam(value = "contactNature", required = true) String contactNature) {
        try {
            Customer customer = customerService.getCustomerNameAndPhone(contactName, contactInformation, contactNature);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), customer);
        } catch (Exception e) {
            log.error("contact getCustomerIdAndContactInformation error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 导入联系人信息
     * @param request
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/importContact")
    public DataResponse importContact(HttpServletRequest request,
                                          @RequestParam MultipartFile file,
                                          @RequestParam String companyId
    ) throws IOException {
        JSONObject jsonObject = new JSONObject();
        try {
            ExcelUtil excelUtil = new ExcelUtil();
            List<ContactImportVO> list = excelUtil.excelImport(file.getInputStream(), ContactImportVO.class);//1.读取前端的excel文件并转换成list集合
            if (list == null || list.size() == 0) {
                return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
            }
            jsonObject = contactService.importContact(list,companyId);

        } catch (Exception e) {
            log.error("contact importContact:", e);
        }
        //返回导入数量、重复数量
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),jsonObject);
    }

    /**
     * 联系人大表数据加入客户联系人下
     * @return
     */
    @PostMapping("/comparisonData")
    public DataResponse comparisonData() {
        try {
            int num = contactService.comparisonData();
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),num);
        } catch (Exception e) {
            log.error("contact comparisonData error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 联系人大表去重
     * @return
     */
    @PostMapping("/updateRepeatData")
    public DataResponse updateRepeatData() {
        try {
            int num = contactService.updateRepeatDatas();
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),num);
        } catch (Exception e) {
            log.error("contact updateRepeatData error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 展示兑换码列表
     * @param contactId 联系人id
     * @return
     */
    @PostMapping("/selectContactExchangeCodeList")
    public DataResponse selectContactExchangeCodeList(@RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                                          @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                          @RequestParam(value = "sortName", required = false) String sortName,
                                          @RequestParam(value = "sortOrder", required = false) String sortOrder,
                                          @RequestParam(value = "contactId", required = false) String contactId) {
        try {
            DhPageInfo<ContactExchangeCodeVO> list = contactService.selectContactExchangeCodeList(pageIndex,pageSize,sortName,sortOrder,contactId);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), list);
        } catch (Exception e) {
            log.error("contact selectContactExchangeCodeList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 联系人发码开关
     * @return
     */
    @PostMapping("/selectParameterSetting")
    public DataResponse selectParameterSetting() {
        try {
            SysParameterSetting parameterSetting = contactService.selectParameterSetting();
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),parameterSetting);

        } catch (Exception e) {
            log.error("contact updateContactcodeStatus error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 开启关闭联系人发码
     * 联系人发码状态  0发码 1不发码
     * @return
     */
    @PostMapping("/updateContactcodeStatus")
    public DataResponse updateContactcodeStatus(@RequestParam(value = "contactCodeStatus", required = false) Integer contactCodeStatus) {
        try {
            Boolean rs = contactService.updateContactcodeStatus(contactCodeStatus);
            if (rs) {
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
            }
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        } catch (Exception e) {
            log.error("contact updateContactcodeStatus error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 开启关闭联系人发码
     * 联系人发码状态  0发码 1不发码
     * @return
     */
    @PostMapping("/contactSendCode")
    public DataResponse contactSendCode(@RequestParam(value = "contactInformation", required = false) String contactInformation,
                                        @RequestParam(value = "contactId", required = false) String contactId) {
        try {
            UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
            String msg = contactService.contactSendCode(contactInformation,userAccount.getAccountId(),contactId,"1");
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),msg);
        } catch (Exception e) {
            log.error("contact updateContactcodeStatus error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 会员卡数据补全
     * @return
     */
    /*@PostMapping("/cardDataCompletion")
    public DataResponse cardDataCompletion() {
        try {
             contactService.cardDataCompletion();
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        } catch (Exception e) {
            log.error("contact cardDataCompletion error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }*/

    /**
     * 查询多出来的会员卡
     * @return
     */
    /*@PostMapping("/cardDataMany")
    public DataResponse cardDataMany() {
        try {
            contactService.cardDataMany();
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        } catch (Exception e) {
            log.error("contact cardDataMany error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }*/


    /**
     * 导入联系人信息
     * @param request
     * @param file
     * @return
     * @throws IOException
     */
    /*@PostMapping("/importCard")
    public DataResponse importCard(HttpServletRequest request,
                                      @RequestParam MultipartFile file
    ) throws IOException {
        JSONObject jsonObject = new JSONObject();
        try {
            ExcelUtil excelUtil = new ExcelUtil();
            List<CardImportVO> list = excelUtil.excelImport(file.getInputStream(), CardImportVO.class);//1.读取前端的excel文件并转换成list集合
            if (list == null || list.size() == 0) {
                return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
            }
            jsonObject = contactService.importCard(list);
            //contactService.cardDataManys(list);


        } catch (Exception e) {
            log.error("contact importContact:", e);
        }
        //返回导入数量、重复数量
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),jsonObject);
    }*/

    /**
     * 会员卡数据所有人添加
     * @return
     */
    /*@PostMapping("/cardAccount")
    public DataResponse cardAccount() {
        try {
             contactService.cardAccount();
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        } catch (Exception e) {
            log.error("contact cardAccount error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }*/


    /**
     * 查询联系人标签
     * @return
     */
    @PostMapping("/contactTagList")
    public DataResponse contactTagList() {
        try {
            List<ContactTag> list = contactDetailService.contactTagList();
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), list);
        } catch (Exception e) {
            log.error("contact contactTagList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }


    /**
     * 新增联系人标签库
     * @return
     */
    @PostMapping("/addContactTag")
    public DataResponse addContactTag(@RequestParam(value = "tag", required = false) String tag) {
        try {
            String result = contactDetailService.addContactTag(tag);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), result);
        }catch(Exception e) {
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 删除联系人标签库
     * @param tagId 联系人标签id
     * @return
     */
    @PostMapping("/deleteContactTag")
    public DataResponse deleteContactTag(@RequestParam(value = "tagId", required = false) String tagId) {
        Boolean result = contactDetailService.deleteContactTag(tagId);
        if (result) {
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        } else {
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 新增跟进计划时间
     * @param qo
     * @return
     */
    @PostMapping("/addContactFollowPlanTimes")
    public DataResponse addContactFollowPlanTimes(@RequestBody ContactFollowPlanTimeQO qo) {
        Boolean result = contactService.addContactFollowPlanTimes(qo);
        if (result) {
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        } else {
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 修改联系人意向状态
     * @param contactId 联系人id
     * @param currentIntention 目前意向   0有意向 1无意向
     * @return
     */
    @PostMapping("/updateContactCurrentIntention")
    public DataResponse updateContactCurrentIntention(@RequestParam(value = "contactId", required = false) String contactId,
                                                      @RequestParam(value = "currentIntention", required = false) String currentIntention) {
        Boolean result = contactService.updateContactCurrentIntention(contactId,currentIntention);
        if (result) {
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        } else {
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 移除计划
     * @param contactId 联系人id
     * @return
     */
    @PostMapping("/updateFollowPlanTime")
    public DataResponse updateFollowPlanTime(@RequestParam(value = "contactId", required = false) String contactId) {
        Boolean result = contactService.updateFollowPlanTime(contactId);
        if (result) {
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        } else {
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }











}
